from django.db import models
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify

from pypsqueak.page.models import Page

class Group(models.Model):
	""" A collection of people """

	title = models.CharField(max_length=255)
	description = models.TextField(blank=True)
	slug = models.SlugField(editable=False, unique=True )
	start_date = models.DateField()
	end_date = models.DateField()


	def __unicode__(self):
		return self.title


	@staticmethod
	def get_insert_url():
		return '/group/insert/'
	def get_absolute_url(self):
		return '/group/%s/' % self.slug
	def get_update_url(self):
		return '/group/%s/update/' % ( self.slug )
	def get_delete_url(self):
		return '/group/%s/delete/' % ( self.slug )


	def save( self ):
		""" Set slug to unique value on first save. """

		# Set slug if we haven't saved the model yet.
		if not self.id:
			# Remove all spaces and special chars.
			self.slug = slugify(self.title)

		# Call save event.
		super(Group, self).save(False, False)


	@staticmethod
	def insert_permission(user):
		""" Can the user insert a new record? """
		return user.is_staff
	def permission(self, user, permission):
		""" Determine if the user has permission """
		if permission=='update' or permission=='delete':
			return user.is_staff
		elif permission=='select':
			return True
		else:
			raise Exception, 'Invalid permission'


	class Admin:
		pass


	class Meta:
		get_latest_by = "end_date"
		ordering = [ "end_date", "title" ]



class Membership(models.Model):
	""" User membership of a group """
	user = models.ForeignKey(User)
	group = models.ForeignKey(Group)

	@staticmethod
	def get_insert_url():
		return '/group/membership/insert/'
	def get_delete_url(self):
		return '/group/membership/%s/delete/' % ( self.pk )

	@staticmethod
	def insert_permission(user):
		""" Can the user insert a new record? """
		return Group.insert_permission(user)
	def permission(self, user, permission):
		""" Determine if the user has permission """
		if permission=='update' or permission=='delete':
			return user.is_staff
		elif permission=='select':
			return True
		else:
			raise Exception, 'Invalid permission'



class Assignment(models.Model):
	""" Used to store a particular item for a group information """

	title = models.CharField(max_length=255)
	description = models.TextField(blank=True)
	order = models.IntegerField(default=0)
	group = models.ForeignKey(Group)
	pages = models.ManyToManyField(Page)
	due_date = models.DateField(blank=True,null=True)


	def __unicode__(self):
		return self.title


	def InitializePage(self, page):
		""" Initialize a page with the assignment """
		page.body += self.description
		page.save()
		self.pages.add(page)
		self.save()


	@staticmethod
	def get_insert_url():
		return '/group/assignment/insert/'
	def get_absolute_url(self):
		return '/group/assignment/%s/' % ( self.pk )
	def get_update_url(self):
		return '/group/assignment/%s/update/' % ( self.pk )
	def get_delete_url(self):
		return '/group/assignment/%s/delete/' % ( self.pk )


	@staticmethod
	def insert_permission(user):
		""" Rely on create permission of owning group. """
		return Group.insert_permission(user)
	def permission(self, user, permission):
		""" Determine if the user has permission """
		return self.group.permission(user, permission)


	class Admin:
		pass


	class Meta:
		get_latest_by = "order"
		ordering = [ "order", "title" ]


